//
//  ClassifyCell.m
//  掌厨
//
//  Created by aoyolo on 15/10/22.
//  Copyright (c) 2015年 李祖铜. All rights reserved.
//

#import "ClassifyCell.h"
#import "UIButton+WebCache.h"

#import "MenuController.h"
#import "RecipeViewController.h"

@interface ClassifyCell ()
@property (nonatomic, strong)NSArray *arrays;

@end
@implementation ClassifyCell


- (void)awakeFromNib {
    CGFloat gap = 20;//所有间隙
    CGFloat btnWidth = (ScreenWidth - gap *5)/4;//button的宽
    for (int i = 0 ; i < 8; i++)
    {
        CGFloat btnX  = (gap + btnWidth)*(i % 4) +gap;
        CGFloat btnY  = (gap + btnWidth)*(i / 4) +gap;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame =CGRectMake(btnX+5, btnY, btnWidth-10, btnWidth-10);
        button.layer.cornerRadius = 22;
        [button addTarget:self action:@selector(menuBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        button.tag = i+1;
        //加载图片
        NSString *str = _arrays[i][@"image"];
        NSURL *url = [NSURL URLWithString:str];
        [button sd_setImageWithURL:url forState:UIControlStateNormal];
        [self addSubview:button];
        
        //加载图标下文字
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(btnX+15, btnY+60, ScreenWidth, 25)];
        titleLabel.text = _arrays[i][@"text"];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.backgroundColor = [UIColor lightTextColor];
        titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:titleLabel];
    }
    
}
- (void)setFile:(NSArray *)ClassifyArray
{
    _arrays = ClassifyArray;
    [self awakeFromNib];

}

-(void)menuBtn:(UIButton *)btn
{
       
    MenuController *menuCtl = [[MenuController alloc]init];
    menuCtl.title =[NSString stringWithFormat:@"%@", _arrays[btn.tag-1][@"text"]];
    menuCtl.hidesBottomBarWhenPushed = YES;//隐藏tabr
    menuCtl.serial_id = btn.tag;
    RecipeViewController *viewCtl = (RecipeViewController *)[self.superview.superview.superview nextResponder];
    [viewCtl.navigationController pushViewController:menuCtl animated:YES];
}

@end
